/*
 * UIService.fx
 *
 * Created on Feb 13, 2010, 11:51:05 AM
 */

package dawnland03.ui.service;

import javafx.scene.paint.Color;
import javafx.util.Math;

import dawnland03.data.map.model.Matrix;
import dawnland03.data.map.model.MatrixDirection;
import dawnland03.service.GeneralFacade;
import dawnland03.ui.model.UIEntity;
import dawnland03.ui.model.UIHexagon;
import dawnland03.ui.model.UIMatrix;
import javafx.scene.Group;
import java.util.Map;
import java.util.HashMap;
import dawnland03.ui.model.entity.UIEntityModel;
import dawnland03.ui.model.UIEntityPanel;

def uiService = UIService {
    };

/*
This is a script function and works like a static function.
Its scope is to return a singleton instance for the UIService
*/
public function getUIService() {
    uiService;
}

public class UIService {
    package var uiMatrix : UIMatrix;

    package var uiEntities : Group = Group{};
    package var uiEntitiesMap : Map = new HashMap(); //key: entity.id, value: the corresponding UIEntity

    package var uiEntityPanel : UIEntityPanel = UIEntityPanel{};
    package var uiEntitiesModelMap : Map = new HashMap(); //key: entity.id, value: the corresponding UIEntityModel

    def UIHEXAGON_VERTICAL_EDGE =40.0; //x
    def UIHEXAGON_PREF_WIDTH = UIHEXAGON_VERTICAL_EDGE *  Math.sqrt(3.0); //x*sqrt3
    //def UIHEXAGON_PREF_WIDTH = UIHEXAGON_VERTICAL_EDGE *  1.7; //x*sqrt3
    def UIHEXAGON_PREF_HEIGHT = 2 * UIHEXAGON_VERTICAL_EDGE; //2*x

    def colors = [Color.web("#094D02"), Color.web("#3D2B01"), Color.web("#AF9F51"), Color.web("#499D91")]; //grass, wood, stone, iron

    init{
      println("init of UIService - this should happen only once.");
    }

    public function getUIHexagonPrefWidth () : Number {
      return UIHEXAGON_PREF_WIDTH;
    }

    public function getXStep() : Number {
        return getUIHexagonPrefWidth() / 2;
    }

    public function getUIHexagonPrefHeight () : Number {
      return UIHEXAGON_PREF_HEIGHT;
    }

    public function getYStep() : Number {
        return getUIHexagonPrefHeight() / 4;
    }

    public function getColor(index: Integer): Color {
      return colors[index];
    }

    public function getAngle(matrixDirection: MatrixDirection): Number {
        if (matrixDirection == MatrixDirection.UP){
            return 0;
        } else if (matrixDirection == MatrixDirection.RIGHT_UP){
            return 30;
        } else if (matrixDirection == MatrixDirection.RIGHT){
            return 90;
        } else if (matrixDirection == MatrixDirection.RIGHT_DOWN){
            return 150;
        } else if (matrixDirection == MatrixDirection.LEFT_DOWN){
            return -150;
        } else if (matrixDirection == MatrixDirection.LEFT){
            return -90;
        } else if (matrixDirection == MatrixDirection.LEFT_UP){
            return -30;
        }
        return 0.0;
    }


    /*
     Factory functions
    */

    public function getUIMatrix() : UIMatrix {
        if(uiMatrix == null){
            uiMatrix = UIMatrix{
                matrix: GeneralFacade.getMatrix();
                uiService : this;
            };
        }
        uiMatrix;
    }

    public function getUIHexagon(matrixCellId : Integer) : UIHexagon {
        getUIMatrix().getUIHexagon(matrixCellId);
    }

    public function getUIEntities() : Group {
        uiEntities;
    }

    public function getUIEntityPanel() : UIEntityPanel {
        uiEntityPanel;
    }

    public function setSelectedUIEntity(entityId: Integer){
       var uiEntityModel = uiEntitiesModelMap.get(entityId) as UIEntityModel;
       uiEntityPanel.uiEntityModel = uiEntityModel;
    }

    public function createUIHexagonsFromMatrix(matrix: Matrix) : UIHexagon[] {
       var uiHexagons : UIHexagon[];
       var uiHexagon : UIHexagon;
       for(matrixCell in matrix.getMatrixCellsAsArray()){
        uiHexagon = UIHexagon {
            matrixCell: matrixCell;
            uiService: this;
            }
        insert uiHexagon into uiHexagons;
        }
       uiHexagons;
    }

    public function createUIEntity(entityId : Integer, matrixCellId : Integer) : UIEntity{
        var uiEntity = getUIEntity(entityId);
        if(uiEntity==null){
            var uiHexagon = getUIHexagon(matrixCellId);
            uiEntity = UIEntity {
                entityId : entityId;
                uiHexagon : uiHexagon;
                uiService : this;
            }
            insert uiEntity into uiEntities.content;
            uiEntitiesMap.put(entityId, uiEntity);
            var uiEntityModel = UIEntityModel {
                id: entityId;
            }
            uiEntitiesModelMap.put(entityId, uiEntityModel);
        }
        uiEntity;
    }

    public function getUIEntity(entityId:Integer) : UIEntity{
        uiEntitiesMap.get(entityId) as UIEntity;
    }

    public function getUIEntityModel(entityId:Integer) : UIEntityModel{
        uiEntitiesModelMap.get(entityId) as UIEntityModel;
    }

}
